package cn.edu.ujn.project.supermarket.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.ujn.project.common.annotation.Log;
import cn.edu.ujn.project.common.core.controller.BaseController;
import cn.edu.ujn.project.common.core.domain.AjaxResult;
import cn.edu.ujn.project.common.core.page.TableDataInfo;
import cn.edu.ujn.project.common.enums.BusinessType;
import cn.edu.ujn.project.common.utils.poi.ExcelUtil;
import cn.edu.ujn.project.supermarket.domain.DealReport;
import cn.edu.ujn.project.supermarket.domain.GoodsInfor;
import cn.edu.ujn.project.supermarket.domain.InportGoods;
import cn.edu.ujn.project.supermarket.domain.PurchaseReport;
import cn.edu.ujn.project.supermarket.extendbean.GoodsInforDeal;
import cn.edu.ujn.project.supermarket.service.IDealReportService;
import cn.edu.ujn.project.supermarket.service.IGoodsInforService;
import cn.edu.ujn.project.supermarket.service.IInportGoodsService;
import cn.edu.ujn.project.supermarket.service.IPurchaseReportService;
import cn.edu.ujn.project.supermarket.service.ISupplierService;
import tool.Generator;

/**
 * 处理记录Controller
 * 
 * @author kkk
 * @date 2023-11-13
 */
@Controller
@RequestMapping("/system/dealreport")
public class DealReportController extends BaseController {
    private String prefix = "system/dealreport";

    @Autowired
    private IDealReportService dealReportService;

    @Autowired
    private IGoodsInforService goodsInforService;

    @Autowired
    private IPurchaseReportService purchaseReportService;

    @Autowired
    private IInportGoodsService inportGoodsService;

    @Autowired
    private ISupplierService supplierService;

    @RequiresPermissions("system:dealreport:view")
    @GetMapping()
    public String dealreport() {
        return prefix + "/dealreport";
    }

    /**
     * 查询处理记录列表
     */
    @RequiresPermissions("system:dealreport:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DealReport dealReport) {
        startPage();
        List<DealReport> list = dealReportService.selectDealReportList(dealReport);
        return getDataTable(list);
    }

    /**
     * 导出处理记录列表
     */
    @RequiresPermissions("system:dealreport:export")
    @Log(title = "处理记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DealReport dealReport) {
        List<DealReport> list = dealReportService.selectDealReportList(dealReport);
        ExcelUtil<DealReport> util = new ExcelUtil<DealReport>(DealReport.class);
        return util.exportExcel(list, "处理记录数据");
    }

    /**
     * 新增处理记录
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存处理记录
     */
    @RequiresPermissions("system:dealreport:add")
    @Log(title = "处理记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DealReport dealReport) {
        return toAjax(dealReportService.insertDealReport(dealReport));
    }

    /**
     * 修改处理记录
     */
    @RequiresPermissions("system:dealreport:edit")
    @GetMapping("/edit/{dealId}/{isApprove}")
    @ResponseBody
    public void edit(@PathVariable("dealId") String dealId, @PathVariable("isApprove") String isApprove, ModelMap mmap) {
        if(isApprove!=null && dealId!=null){
            DealReport dealReport = dealReportService.selectDealReportByDealId(dealId);
            dealReport.setDealPerson(String.valueOf(getUserId()));
            dealReport.setAccept(isApprove);
            dealReport.setDealDate(new Date());
            System.out.println(dealReport);
            if (isApprove.equals("approve")) {
                // add quantity to goods information
                PurchaseReport temp = new PurchaseReport();
                temp.setPurchaseReportId(dealReport.getPurchaseReportId());
                List<PurchaseReport> purchaseReports = this.purchaseReportService.selectPurchaseReportList(temp);
//            System.out.println(purchaseReports);
                for (PurchaseReport item : purchaseReports) {
                    GoodsInfor goodsInfor = this.goodsInforService.selectGoodsInforByBarCode(item.getBarCode());
                    Integer originalQuantity = goodsInfor.getQuantity();
                    goodsInfor.setQuantity(originalQuantity + item.getQuantity());
//                System.out.println(goodsInfor);
                    this.goodsInforService.updateGoodsInfor(goodsInfor);
                }
                // add import goods
                String importId = Generator.getString();
                for (PurchaseReport item : purchaseReports) {
                    InportGoods inportGoods = new InportGoods();
                    inportGoods.setInportId(importId);
                    inportGoods.setBarCode(item.getBarCode());
                    inportGoods.setDate(new Date());
                    inportGoods.setQuantity(item.getQuantity());
                    inportGoods.setSupplierCode(item.getSupplierId());
                    this.inportGoodsService.insertInportGoods(inportGoods);
                }
            }
            mmap.put("dealReport", dealReport);
            dealReportService.updateDealReport(dealReport);
        }

    }

    /**
     * 修改保存处理记录
     */
    @RequiresPermissions("system:dealreport:edit")
    @Log(title = "处理记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DealReport dealReport) {
        return toAjax(dealReportService.updateDealReport(dealReport));
    }

    /**
     * 删除处理记录
     */
    @RequiresPermissions("system:dealreport:remove")
    @Log(title = "处理记录", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(dealReportService.deleteDealReportByDealIds(ids));
    }

    /**
     * 审阅
     */
    @GetMapping("/review/{purchasereportid}")
    public String review(@PathVariable("purchasereportid") String purchaseReportId, ModelMap mmap) {
        mmap.put("purchaseReportId", purchaseReportId);
        return prefix + "/review";
    }

    @PostMapping("/listgoods/{id}")
    @ResponseBody
    public TableDataInfo listGoods(@PathVariable("id") String purchaseReportId) {
        System.out.println("post方法");
        startPage();
        List<GoodsInforDeal> glist = new ArrayList<GoodsInforDeal>();
        PurchaseReport temp = new PurchaseReport();
        temp.setPurchaseReportId(purchaseReportId);
        List<PurchaseReport> list = purchaseReportService.selectPurchaseReportList(temp);
        for (PurchaseReport purchaseReport : list) {
            GoodsInfor g = goodsInforService.selectGoodsInforByBarCode(purchaseReport.getBarCode());
            String supplierId = purchaseReport.getSupplierId();
            String supplierName = supplierService.selectSupplierBySupplierId(supplierId).getName();
            glist.add(new GoodsInforDeal(purchaseReport.getQuantity(),
                    supplierId, supplierName, g));
        }
        return getDataTable(glist);
    }
}